const axios = require("../../utils/request")
const constant = require("../../constant/constant")
Page({
    data: {
        dataSource: {},
        tabActiveName: "热销好货",
        tabActiveIndex: 0,
        headerHeight: "",
        contentHeight: "",
        cartNum: 0,
    },
    onLoad: function () {
        // 获取缓存信息
        const result = wx.getStorageSync(constant.storageCartKey)
        console.log("result", result)
        wx.showLoading()
        axios("/home/info", "GET").then(res => {
            wx.hideLoading()
            res.data.recommend_info.forEach(item => {
                item.tab_list.forEach(it => {
                    if (result) {
                        it.cart_num = 0
                        result.forEach(str => {
                            if (it.goods_id === str.goods_id) {
                                it.cart_num = str.cart_num
                            }
                        })
                    } else {
                        it.cart_num = 0
                    }
                })
            })
            this.setData({
                dataSource: res.data,
                cartNum: result ? result.length : 0
            })
        }).catch(err => {
            wx.hideLoading()
        })

        const query = wx.createSelectorQuery()
        const that = this
        query.selectAll('.header').boundingClientRect(function (obj) {
            console.log("obj", obj);
            console.log(obj[0].height + 'px')
            that.setData({
                contentHeight: "calc(100% - " + obj[0].height + "px - 100rpx)"
            })
        }).exec();
    },
    handleSelect: function (event) {
        const index = this.data.dataSource.recommend_info.findIndex(x => x.tab_name === event.currentTarget.dataset.name)
        this.setData({
            tabActiveName: event.currentTarget.dataset.name,
            tabActiveIndex: index
        })
    },
    handleAddCart: function (e) { // 点击加入购物车
        console.log("e", e.detail)
        // 购物车缓存
        const cartInfo = wx.getStorageSync(constant.storageCartKey)
        let arr = cartInfo || []
        let obj = {}
        this.data.dataSource.recommend_info.forEach(item => {
            item.tab_list.forEach(it => {
                if (it.goods_id === e.detail) {
                    it.cart_num = 1
                    obj = it
                    this.setData({
                        dataSource: this.data.dataSource
                    })
                }
            })
        })
        arr.push(obj)
        wx.setStorageSync(constant.storageCartKey, arr)
        this.setData({
            cartNum: arr.length
        })
    },
    // 点击购物车的减号按钮
    hadleReduceGoodsNum: function (e) {
        console.log("e", e.detail)
        const data = e.detail
        const that = this
        if (data.cart_num < 2) {
            wx.showModal({
                content: '确认删除这个商品么',
                cancelText: '我再想想',
                confirmText: '确认删除',
                success(res) {
                    if (res.confirm) {
                        data.cart_num--
                        that.actionCart(data)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            data.cart_num --
            this.actionCart(data)
        }
        this.actionCart(data)
    },
    // 点击购物车的加号
    handleAddGoodsNum: function (e) {
        const data = e.detail
        console.log("data", data)
        if (data.cart_num < 10) {
            data.cart_num++
            this.actionCart(data)
        } else {
            wx.showToast({
              title: '该商品已达到当前购买上限',
              icon: 'none',
              duration: 1000
            })
        }
    },
    actionCart: function (row) { // 点击购物车的增加或者减少公共提取
        const cartInfo = wx.getStorageSync(constant.storageCartKey)
        cartInfo.forEach((item, index) => {
            if (item.goods_id === row.goods_id) {
                item.cart_num = row.cart_num
            }
            if(item.cart_num === 0) {
                cartInfo.splice(index, 1)
            }
        })
        wx.setStorageSync(constant.storageCartKey, cartInfo)
        this.setData({
            cartNum: cartInfo.length
        })
        this.data.dataSource.recommend_info.forEach(item => {
            item.tab_list.forEach(it => {
                if (it.goods_id === row.goods_id) {
                    it.cart_num = row.cart_num
                    this.setData({
                        dataSource: this.data.dataSource
                    })
                }
            })
        })

    }
})